﻿package fpc.changers;
import haxe.unit.TestCase;

class ChangerTest extends TestCase
{
	public function new() 
	{
		super();
	}
	
	var _changer:Changer<Int>;
	
	public override function setup()
	{
		_changer = new Changer();
	}
	
	// CALLBACK
	
	var _callback_value:Int;
	
	public function testCallback()
	{
		_changer.add(callbacks_onChange);
		_changer.value = 10;
		assertEquals(10, _callback_value);
		
		_changer.remove(callbacks_onChange);
		_changer.value = 50;
		assertEquals(10, _callback_value);
	}
	
	function callbacks_onChange(value:Int)
	{
		assertEquals(10, value);
		assertEquals(10, _changer.value);
		
		_callback_value = value;
		
		_changer.value = 20;
		assertEquals(20, _changer.value);
	}
	
	// CALLBACKS CHAIN
	
	var _callbacksChain_value0:Int;
	
	var _callbacksChain_value1:Int;
	
	var _callbacksChain_value2:Int;
	
	public function testCallbacksChain()
	{
		_changer.add(callbacksChain_onChange0);
		_changer.add(callbacksChain_onChange1);
		_changer.add(callbacksChain_onChange2);
		_changer.value = 10;
		
		assertEquals(10, _callbacksChain_value0);
		assertEquals(10, _callbacksChain_value1);
		assertEquals(10, _callbacksChain_value2);
		
		callbacksChain_onChange0(20);
		
		assertEquals(20, _callbacksChain_value0);
		assertEquals(20, _callbacksChain_value1);
		assertEquals(20, _callbacksChain_value2);
		
		callbacksChain_onChange1(30);
		
		assertEquals(30, _callbacksChain_value0);
		assertEquals(30, _callbacksChain_value1);
		assertEquals(30, _callbacksChain_value2);
		
		callbacksChain_onChange1(50);
		
		assertEquals(50, _callbacksChain_value0);
		assertEquals(50, _callbacksChain_value1);
		assertEquals(50, _callbacksChain_value2);
	}
	
	public function callbacksChain_onChange0(value)
	{
		_changer.value = value;
		_callbacksChain_value0 = value;
	}
	
	public function callbacksChain_onChange1(value)
	{
		_changer.value = value;
		_callbacksChain_value1 = value;
	}
	
	public function callbacksChain_onChange2(value)
	{
		_changer.value = value;
		_callbacksChain_value2 = value;
	}
	
	// INTERACTION
	
	var _interaction_changer0:Changer<Int>;
	
	var _interaction_changer1:Changer<Int>;
	
	var _interaction_value:Int;
	
	var _interaction_value0:Int;
	
	var _interaction_value1:Int;
	
	public function testInteraction()
	{
		_interaction_changer0 = new Changer<Int>();
		_interaction_changer1 = new Changer<Int>();
		
		_changer.add(_interaction_onChange);
		_interaction_changer0.add(_interaction_onChange0);
		_interaction_changer1.add(_interaction_onChange1);
		
		_interaction_value = 0;
		_interaction_value0 = 0;
		_interaction_value1 = 0;
		
		_interaction_changer0.linkTo(_changer);
		_changer.value = 10;
		assertEquals(10, _interaction_value);
		assertEquals(10, _interaction_value0);
		assertEquals(0, _interaction_value1);
		
		_interaction_changer1.linkTo(_changer);
		_changer.value = 20;
		assertEquals(20, _interaction_value);
		assertEquals(20, _interaction_value0);
		assertEquals(20, _interaction_value1);
		
		_interaction_changer1.unlink();
		_changer.value = 30;
		assertEquals(30, _interaction_value);
		assertEquals(30, _interaction_value0);
		assertEquals(20, _interaction_value1);
		
		_changer.unlink();
		_changer.value = 50;
		assertEquals(50, _interaction_value);
		assertEquals(30, _interaction_value0);
		assertEquals(20, _interaction_value1);
		
		_interaction_changer0.value = 70;
		_interaction_changer1.value = 100;
		assertEquals(50, _interaction_value);
		assertEquals(70, _interaction_value0);
		assertEquals(100, _interaction_value1);
		
		_changer.linkTo(_interaction_changer0);
		_changer.linkTo(_interaction_changer1);
		_changer.value = 110;
		_interaction_changer0.value = 150;
		_interaction_changer1.value = 200;
		assertEquals(200, _interaction_value);
		assertEquals(200, _interaction_value0);
		assertEquals(200, _interaction_value1);
	}
	
	function _interaction_onChange(value)
	{
		_interaction_value = value;
	}
	
	function _interaction_onChange0(value)
	{
		_interaction_value0 = value;
	}
	
	function _interaction_onChange1(value)
	{
		_interaction_value1 = value;
	}
}